//
//  NSDIctionary+Safe
//  EgovaMobile
//
//  Created by jiehaijun on 16/3/30.
//  Copyright © 2016年 Facebook. All rights reserved.
//

#import "NSDictionary+Safe.h"

@implementation NSDictionary(Safe)

- (id)NoNullObjectForKey:(NSString *)key{
    id result = [self objectForKey:key];
    if (result == [NSNull null]) {
        return nil;
    }
    
    return result;
}

- (id)objectForKeySafe:(NSString *)key
{
    id result = [self objectForKey:key];
    if (result == [NSNull null]) {
        return nil;
    }
    
    return result;
}

/*!
 * @brief 把格式化的JSON格式的字符串转换成字典
 * @param jsonString JSON格式的字符串
 * @return 返回字典
 */
+ (id)dictionaryWithJsonString:(NSString *)jsonString {
    if (jsonString == nil) {
        return nil;
    }
    
    NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    NSError *err;
    id dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                        options:NSJSONReadingMutableContainers
                                                          error:&err];
    if(err) {
        NSLog(@"json解析失败：%@",err);
        return nil;
    }
    return dic;
}

@end
